<?php $this->widget('application.widgets.Highlighter');?>
<script type="text/javascript">
var close_overlay = function(){
    $("#overlay_bc").fadeOut(200);
    $("#overlay_box").hide();   
}

var updateCache = function(){
    var data = $('#post-form').serialize();
    setInterval(function(){
        var serialize = $('#post-form').serialize();
        if (data == serialize) return;
        data = serialize;
        $("#cache img").show();
        $.ajax({
            url:"<?php echo $this->createUrl('tempcache', array('id' => $model->id))?>",
            type:"post",
            data:data,
            dataType:'json',
            success:function(data){
                $("#cache span").html("自动保存时间：" +data.time);
                $("#cache img").hide();
                $("#cache a").show();
            }
        });
    }, 5000);
    if ($("#cache span").html() == '') {$("#cache a").hide();}
}

$(function(){
    var height = $(window).height() - 80;
    //view box
    var note = $("#overlay_box .note");
    var form = $("#overlay_box .form");
    $(".title").live('click', function(){
        $("#overlay_bc").fadeIn(200);
        $("#overlay_box").show();
        note.html('loading...').show();
        form.hide();
        $.get($(this).attr('href'), {}, function(data){
            note.replaceWith($(data).find(".note"));
            note = $("#overlay_box .note");
            if (note.height() > height){
                note.height(height);
            }
            SyntaxHighlighter.highlight()
        })
        return false;
    });
    //edit box
    $("#create").click(function(){
        $("#overlay_bc").fadeIn(200);
        $("#overlay_box").show();
        note.hide();
        form.html('waiting...').show();
        $.get($(this).attr('href'), {}, function(data){
            form.replaceWith($(data).find(".form"));
            form = $("#overlay_box .form");
            if (form.height() > height){
                form.height(height);
            }
            updateCache();
        })
        return false;
    });
})

function cleanConfirm(){
    return confirm("确定要清除缓存吗？") ? true : false;
}
</script>

<?php
$this->breadcrumbs=array(
	'Notes'=>array('index'),
	'Manage',
);

$this->menu=array(
	array('label'=>'List Notes', 'url'=>array('index')),
	array('label'=>'Create Notes', 'url'=>array('create'), 'linkOptions' => array('id' => 'create')),
);


Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
	$('.search-form').toggle();
	return false;
});
$('.search-form form').submit(function(){
	$.fn.yiiGridView.update('post-grid', {
		data: $(this).serialize()
	});
	return false;
});
");
?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'post-grid',
	'cssFile' => false,
	'dataProvider'=>$model->search(),
	'filter'=>$model,    //过滤
    'template' => "{items}\n{summary}\n{pager}",
	'columns'=>array(
		array(
		    'name' => 'id',
		    'htmlOptions' => array('width' => 25)
		),
		array(
			'name' => 'cid',
		    'filter' => CHtml::listData($model->categoryData->findAll(), 'id', 'name'),
			'value' => '$data->category->name'
		),
		array(
		    'name' => 'title',
		    'type' => 'html',
		    'value' => 'CHtml::link($data->title, array(\'view\', \'id\' => $data->id), array(\'class\' => \'title\'))'
		),
		'tag',
		array(
		    'name' => 'top',
		    'type' => 'html',
		    'htmlOptions' => array('width' => 25),
		    'value' => '($data->top) ? CHtml::image(Helper::getUrl("images/yes.gif")) : ""'
		),
		array(    // related add_time
		    'name' => 'add_time',
		    'htmlOptions' => array('width' => 120),
			'value' => 'strtotime($data->add_time)',
		    'type' => 'datetime',
		),
		array(
		    'header' => 'options',
			'class'=>'CButtonColumn',
		),
	),
)); ?>

<div id="overlay_bc"></div>
<div id="overlay_box">
    <div id="container">
        <div id="button"><a href="javascript:close_overlay()" >close</a></div>
        <div class="note"></div>
        <div class="form"></div>
    </div>
</div>